package cn.edu.zime.bigdata.controller;

import cn.edu.zime.bigdata.anotations.AnatationTest;
import cn.edu.zime.bigdata.dao.mapper.StudentMapper;
import cn.edu.zime.bigdata.dao.model.CountValue;
import cn.edu.zime.bigdata.dao.model.Student;
import cn.edu.zime.bigdata.dao.model.StudentGradeStat;
import cn.edu.zime.bigdata.dao.model.Studentss;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
public class StudentController {
    @Autowired
    StudentMapper studentService;
    @GetMapping("/addstudent")
    @AnatationTest
    public int insertStudent(@RequestParam(value = "key1") String id,
                            @RequestParam(value = "key2") String name,
                            @RequestParam(value = "key3") String genda,
                            @RequestParam(value = "key4") String level,
                            @RequestParam(value = "key5") String depart,
                            @RequestParam(value = "key6") String ability,
                            @RequestParam(value = "key7") String grade,
                            @RequestParam(value = "key8") String dengji) {
        System.out.printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", id, name, genda, level, depart, ability,grade,dengji);
        System.out.println("hhha");
        return studentService.insertStudent(new Student(id, name, genda, level, depart, ability,grade,dengji));
//        return studentMapperXML.insertStudent(new Student(id, name, genda, level, depart, ability,grade,dengji));
    }

    @AnatationTest
    @GetMapping("/getAllStudents")
    public List<Student> selectAllStudents() {
        System.out.println("-----select--------");
        return studentService.selectAllStudents();
    }


    @GetMapping("/getStudents")
    public List<Student> selectStudents(@RequestParam(value = "key1") String input) {
        System.out.println("input:" + input);
        return studentService.selectStudents(input, input);
    }

    @GetMapping("/updateStudent")
    public int updateStudent(@RequestParam(value = "key1") String id,
                            @RequestParam(value = "key2") String name,
                            @RequestParam(value = "key3") String genda,
                            @RequestParam(value = "key4") String level,
                            @RequestParam(value = "key5") String depart,
                            @RequestParam(value = "key6") String ability,
                             @RequestParam(value = "key7") String grade,
                             @RequestParam(value = "key8") String dengji) {
        System.out.println("-----update--------");
//        return studentService.updateStudent(new Student(id, name, genda, level, depart, ability,grade,dengji));
        return 1;
    }

    @GetMapping("/deletestudent")
    public int studentDelete(@RequestParam(value = "key1") String id) {
        System.out.println("---------delete---------");
        return studentService.deleteStudent(id);
    }

    @PostMapping("/addpersons1")
    public String upload(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();   //获取文件名称
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  //获取文件类型
        String filePath = "d:/file/";
        fileName = filePath + fileName;   //生成一个唯一的文件名称
        File dest = new File(fileName);  //新建file对象
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);   //将前端传送的文件存储到dest位置
            return "上传成功";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "上传失败";
    }

    @PostMapping("/addpersons")
    public String addStudents(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        //String suffixName = fileName.substring(fileName.lastIndexOf("."));
        Workbook workbook = null;   //定义一个表文件
        workbook = new XSSFWorkbook(file.getInputStream());  //打开.xls类型的文件
        Sheet sheet = workbook.getSheetAt(0);  //打开文件中的第1个sheet
        int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();  //获取总行数
        for (int i = 1; i < physicalNumberOfRows; i++) {   //第一行是属性名，数据记录是从第2行开始
            System.out.println("hangshu:"+ physicalNumberOfRows);
            Row row = sheet.getRow(i);  //获取第（i+1）行数据
            if (row == null) {
                continue;
            }
            System.out.println(row);
            row.getCell(1).setCellType(CellType.STRING);
            String id = row.getCell(1).getStringCellValue();

            System.out.println(id);
            String name = row.getCell(2).getStringCellValue();
            System.out.println(name);
            String sex = row.getCell(3).getStringCellValue();
            System.out.println(sex);
            String level = row.getCell(4).getStringCellValue();
            System.out.println(level);
            String group = row.getCell(5).getStringCellValue();
            System.out.println(group);
            row.getCell(6).setCellType(CellType.STRING);
            String ability = row.getCell(6).getStringCellValue();
            System.out.println(ability);
            row.getCell(7).setCellType(CellType.STRING);
            String grade = row.getCell(7).getStringCellValue();
            System.out.println(grade);
            String dengji = row.getCell(8).getStringCellValue();
            System.out.println(dengji);
            insertStudent(id,name,sex,level,group,ability,grade,dengji);
        }
        return "sucess";
    }



    @GetMapping("/downloadPersonModel")
    public ResponseEntity<byte[]> fileDownload() {
        System.out.println("-----download--------");
        String dirPath = "D:/";
        String filename = "students.xls";
        File file = new File(dirPath + File.separator + filename);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachment", filename);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        try {
            return new ResponseEntity<>(FileUtils.readFileToByteArray(file),
                    headers, HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<byte[]>(e.getMessage().getBytes(), HttpStatus.EXPECTATION_FAILED);
        }
    }

    @GetMapping("/getGendaRatio")
    public List<CountValue> getGendaRatio(){
        return studentService.selectGendaRatio();
    }
    @GetMapping("/getddStudents")
    public List<Studentss> selectddStudents() {
        System.out.println("-----select");
        return studentService.selectddStudents();
    }

}
